package webStudio7i.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import webStudio7i.modelo.Usuario;
import webStudio7i.negocio.UsuarioBll;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String id = request.getParameter("txtusuario");
		 String clave = request.getParameter("txtpassword");
		 UsuarioBll objbll = new UsuarioBll();
		 System.out.println("antes de entrar al try");
		 
		 
		 try {
		 
			 Usuario usuario = new Usuario();
			 usuario.setLogin(id);
			 usuario.setPassword(clave);
			 
			 Usuario a = objbll.login(usuario);
			 System.out.println("metiendo el usuario a la variable usuario a "); 
			 //HttpSession session = request.getSession();
			 System.out.println("sesion creada "); 
			 if(a != null){
				 request.setAttribute("USUARIO_ACTUAL", a);
				 
				 System.out.println("le seteamos e atributo "); 
				 
				if ( a.getTipoUsuario().toString().equals("A")){
					 response.sendRedirect("home_admin.html");
					
					 System.out.println("si es administrador "); 
				}
				else
				     {
						 response.sendRedirect("home_usuario.html");	
						 
						 System.out.println("si es cliente "); 
				     }
			 }
			else {
				 
				response.sendRedirect("login.jsp");
				 System.out.println("ingreso mal los datos  "); }
			 }
	
		catch (Exception e)		{
			e.printStackTrace();	
			//response.sendRedirect("login.jsp");
			
			
			request.setAttribute("rpta", "0");
			RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
			rd.forward(request, response);
			return;
		 }	 
		}
				
	}

